#!/usr/bin/python

"""hidcheck.py
Copyright (C) 2009  Ryan Box

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA."""

__author__ = "Ryan Box (coffebean@sbcglobal.net)"
__version__ = "$Revision: 0.0.1 $"
__date__ = "$Date: 12/01/09 $"
__license__ = "Python"

import os
import glob
import shutil

########################################################################################################################
#Check incrementsdir for hidden files and directories
#This block can be used from an interactive shell
#Will run if config.ini value "keephidden" equals True

def check(incdir='os.listdir()'):
	root = incdir + '/' # The directory created at run time for increments
	file_list = []

	for folder in glob.glob(root):
	        #file list filled with hidden files and directories
	        for file in glob.glob(folder + '.*'):
			file_tuple = file
			file_list.append(file_tuple)

	file_list.sort()
	print '\n'
	print ("filename:")
	print '#'*60  # just vanity
	print '\n'
	for file in file_list:
	    	# extract just the filename
	    	folder, file_name = os.path.split(file)
	    	print "%-40s" % (file_name)

	if file_list != []:
        	for file in file_list:
        	        folder, file_name = os.path.split(file[1])
        	        x = file
        	        print '\n'
        	        print 'These hidden files have been moved to the increments dir:'
        	        ask = raw_input("Do you want to keep the hidden files and directories (y or n 'default yes'): ")
        	        if ask == 'y':
        	                for file in file_list:
        	                        folder, file_name = os.path.split(file[1])
        	                        x = file
        	                        if os.path.isdir(x):
                                        	#print x
	                                        shutil.rmtree(x)
                                	else:
                                        	os.path.isfile(x)
                                        	#print x
                                        	os.remove(x)
        	                break
        	        elif ask == 'n':
        	                print '\n'
        	                print 'Saving incremental hidden files and folders:'
        	                break
        	        else:
        	                print '\n'
        	                print 'You did not answer y or n so the files and directories are being saved'
        	                break
	else:
        	print "There are no Hidden files or directroies."
	print '\n'
	print '#'*60  # just vanity
	print "Hidden file check is done"
	print '\n'
#
#################################################################################################################################

#################################################################################################################################
#

def croncheck(incdir='os.listdir()'):

	root = incdir + '/' # The directory created at run time for increments
	file_list = []

	for folder in glob.glob(root):
		#file list filled with hidden files and directories
		for file in glob.glob(folder + '.*'):
			file_tuple = file
			file_list.append(file_tuple)

			file_list.sort()
			print '\n'
			print ("filename:")
			print '#'*60  # just vanity
			print '\n'
			for file in file_list:
				# extract just the filename
				folder, file_name = os.path.split(file)
				print "%-40s" % (file_name)

				if file_list != []:
					for file in file_list:
						folder, file_name = os.path.split(file[1])
						x = file
						print '\n'
						for file in file_list:
							folder, file_name = os.path.split(file[1])
							x = file
							if os.path.isdir(x):
								#print x
								shutil.rmtree(x)
							else:
								os.path.isfile(x)
								#print x
								os.remove(x)
								break
				else:
					print "There are no Hidden files or directroies."
					print '\n'
					print '#'*60  # just vanity
					print "Hidden file check is done"
					print '\n'
#
#################################################################################################################################